package com.hx.order.service.impl;
import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hx.common.result.PageResult;
import com.hx.order.mapper.DrugMapper;
import com.hx.order.pojo.Drug;
import com.hx.order.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class DrugServiceImpl implements DrugService {

    @Autowired
    private DrugMapper drugMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Drug> findAll() {
        return drugMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Drug> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Drug> drugs = (Page<Drug>) drugMapper.selectAll();
        return new PageResult<Drug>(drugs.getTotal(),drugs.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Drug> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return drugMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Drug> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Drug> drugs = (Page<Drug>) drugMapper.selectByExample(example);
        return new PageResult<Drug>(drugs.getTotal(),drugs.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Drug findById(Long id) {
        return drugMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param drug
     */
    public void add(Drug drug) {
        drugMapper.insert(drug);
    }

    /**
     * 修改
     * @param drug
     */
    public void update(Drug drug) {
        drugMapper.updateByPrimaryKeySelective(drug);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        drugMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateDrugNum(Integer num, String id) {

    }

    @Override
    public void addDrugNum(Integer num, String id) {

    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Drug.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // drug_name
            if(searchMap.get("drugName")!=null && !"".equals(searchMap.get("drugName"))){
                criteria.andLike("drugName","%"+searchMap.get("drugName")+"%");
            }

            // sn
            if(searchMap.get("sn")!=null ){
                criteria.andEqualTo("sn",searchMap.get("sn"));
            }

        }
        return example;
    }

}
